package checkers3d.logic;

import checkers3d.presentation.GUIButton;
import checkers3d.presentation.GUIContainer;
import checkers3d.presentation.GUILabel;
import checkers3d.presentation.GUIPictureBox;
import checkers3d.presentation.GUITextbox;
import checkers3d.presentation.GameEngine;
import checkers3d.presentation.IDrawable;
import checkers3d.presentation.IInputObserver;
import checkers3d.presentation.UtilGUI;
import checkers3d.storage.DataManagerAccounts;
import checkers3d.storage.DataManagerStats;
import java.awt.Color;
import java.awt.Point;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rherbzi
 * @author Ruben Acuna
 */
public class MenuViewStats  extends GUIContainer{
    private final int CONTROL_BUTTON_AXIS = GameEngine.WINDOW_SIZE.y - 100;
    private final String promptStats = "Verify your account to view statistics.";
    private final String promptOpponentStats = "Enter an Opponent's name to view specific statistics.";

    GUITextbox loginName;
    GUITextbox loginPassword;
    GUILabel loginValidation;
    GUITextbox opponentName;
    GUILabel statsGeneral;
    GUILabel statsOpponent;

    public MenuViewStats(Point size){
        super(size);

        LinkedList<IDrawable> buttons = new LinkedList<IDrawable>();

        //add background image.
        addBackground(new GUIPictureBox("background.png", new Point (0, 0)));

        //button to show change stats page.
        buttons.add(new GUIButton(".\\images\\button-stats-administration.png", null, new ObserverAdminLogin()));

        //create the return button with behavior.
        buttons.add(new GUIButton("button-return.png", null, new ObserverReturn()));

        //add other GUI elements. +112 is for move to 1024x768 from 800x600.
        add(new GUILabel("Player Login:", Color.WHITE, new Point(124 + 112, 230)));
        add(new GUILabel("Name:", Color.WHITE, new Point(124 + 112, 250)));
        add(new GUILabel("Password:", Color.WHITE, new Point(102 + 112, 270)));
        loginValidation = new GUILabel("Ready", Color.RED, new Point(207 + 112, 230));
        add(loginValidation);

        add(new GUILabel("Oponent Name:", Color.WHITE, new Point(476 + 112, 230)));
        add(new GUILabel("Stats:", Color.WHITE, new Point(124 + 112, 302)));
        add(new GUILabel("OponentStats:", Color.WHITE, new Point(476 + 112, 302)));

        statsGeneral = new GUILabel(promptStats, Color.WHITE, new Point(124 + 112, 320));
        add(statsGeneral);
        statsOpponent = new GUILabel(promptOpponentStats, Color.WHITE, new Point(476 + 112, 320));
        add(statsOpponent);

        //add the textbox for player name and password.
        loginName = new GUITextbox(new Point(166 + 112, 250), 25, this);
        loginPassword = new GUITextbox(new Point(166 + 112, 270), 25, this);
        loginPassword.setSecure(true);

        //add the textbook for opponent name.
        opponentName = new GUITextbox(new Point(476 + 112, 250), 25, this);

        //add buttons to GUIContainer.
        for(IDrawable component : buttons)
            add(component);

        //compute the positions of the buttons
        UtilGUI.computeLayoutSpanning(getSize(), CONTROL_BUTTON_AXIS, buttons);
    }

    private void validatePlayers()
    {
        loginValidation.setColor(Color.RED);

        try
        {
            String username = loginName.getText();
            String password = loginPassword.getText();

            if(DataManagerAccounts.doesPlayerExist(username))
                if((DataManagerAccounts.getPlayerPassword(username)).equals(password))
                    loginValidation.setColor(Color.GREEN);
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(MenuViewStats.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
   
    @Override
    public void onKeyPress(char key)
    {
        super.onKeyPress(key);

        validatePlayers();

        if(loginValidation.getColor() == Color.GREEN)
        {
            try
            {
                //display the users statisics
                Player player = DataManagerAccounts.loadPlayer(loginName.getText());
                String wins = DataManagerStats.getWins(player);
                String losses = DataManagerStats.getLosses(player);
                String ties = DataManagerStats.getTies(player);
                statsGeneral.setText("Wins: " + wins + "\t Losses: " + losses + "\t Ties: " + ties);

                //if a valid opponent is selected, list versus stats.
                if(DataManagerAccounts.doesPlayerExist(opponentName.getText()))
                {
                    String wins2 = DataManagerStats.getPlayerWins(loginName.getText(), opponentName.getText());
                    String losses2 = DataManagerStats.getPlayerLosses(loginName.getText(), opponentName.getText());
                    String ties2 = DataManagerStats.getPlayerTies(loginName.getText(), opponentName.getText());
                    statsOpponent.setText("Wins: " + wins2 + "\t Losses:" + losses2 + "\t Ties:" + ties2);
                }
                else
                    statsOpponent.setText(promptOpponentStats);
            }
            catch (FileNotFoundException ex)
            {
                Logger.getLogger(MenuViewStats.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            statsGeneral.setText(promptStats);
            statsOpponent.setText(promptOpponentStats);
        }
    }
    
    /**
     * return to the previous page
     */
    private class ObserverReturn implements IInputObserver
    {
        /**
         * Returns the loginName to the main menu screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            GameMaster.getInstance().removeTopGUIContainer();
        }

       /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    /**
     * Opens a window for an admin to login.
     */
    private class ObserverAdminLogin implements IInputObserver
    {
        /**
         * Open a login page for admins.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
           GameMaster.getInstance().addGUIContainer(new MenuAdminLogin(getSize()));
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }
}